package com.turing.post.tuxedo.util;

import javax.ejb.ApplicationException;

import com.turing.post.util.*;

/**
 * 商户交易接入子系统应用异常定义
 * 
 * @author yxb
 * 
 */
@SuppressWarnings("serial")
@ApplicationException(rollback = true)
public class TuxedoApplicationException extends PCPSApplicationException {

	/**
	 * 返回子系统名称
	 */
	public String getSubSysName() {
		return PCPSTSubSys.SUBSYS_TUXEDO;
	}

	/**
	 * 返回子系统错误码简洁描述
	 * 
	 * @return
	 */
	public String getSubSysSysErrCodeDesc() {
		return TuxedoRespCode.getRespCodeMessage(getSubSysErrCode());
	}

	/**
	 * TuxedoApplicationException 异常
	 * 
	 * @param errcode_category
	 *            主错误码
	 * @param subsys_errcode
	 *            子系统错误码
	 * @param errmsg
	 *            错误信息
	 */
	public TuxedoApplicationException(String errcode_category,
			String subsys_errcode, String errmsg) {
		super(errcode_category, subsys_errcode, errmsg);
	}

	/**
	 * CARDApplicationException 应用异常
	 * 
	 * @param subsys_errcode
	 *            主错误码
	 * @param errmsg
	 *            错误信息
	 */
	public TuxedoApplicationException(String subsys_errcode, String errmsg) {
		super(ERRCODE_CATEGORY_APPLICATION, subsys_errcode, errmsg);
	}

	/**
	 * TuxedoApplicationException 应用异常
	 * 
	 * @param subsys_errcode
	 *            错误信息
	 */
	public TuxedoApplicationException(String subsys_errcode) {
		super(ERRCODE_CATEGORY_APPLICATION, subsys_errcode, ERRMSG_UNKNOWN);
	}

	public static String getMappedRespCode(PCPSApplicationException pae) {
		if (pae.getErrCodeCategory().equals(
				PCPSApplicationException.ERRCODE_CATEGORY_DATABASE))
			return (TuxedoRespCode.RC_DBERR);
		else if (pae.getErrCodeCategory().equals(
				PCPSApplicationException.ERRCODE_CATEGORY_SYS))
			return (TuxedoRespCode.RC_SYS);
		else if (pae.getErrCodeCategory().equals(
				PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION)) {
			if (pae instanceof TuxedoApplicationException)
				return pae.getSubSysErrCode();
			String errcode = TuxedoRespCode.getMappedRespCode(pae
					.getSubSysName()
					+ "_" + pae.getSubSysErrCode());
			if (errcode == null || errcode.length() == 0)
				return (TuxedoRespCode.RC_PACKET);
			return (errcode);
		}

		return (TuxedoRespCode.RC_PACKET);
	}
}
